package com.cq.hd.order.api.excel;

import com.alibaba.excel.write.handler.AbstractRowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.*;

import java.util.stream.IntStream;

/**
 * @ClassName CustomWriteHandler
 * @Description 自定义头
 * @Author GTY
 * @Date 2023/2/21 下午3:39
 * @Version 1.0
 */
public class CustomWriteHandler extends AbstractRowWriteHandler {

    /**
     * 行处理后
     * 第二行头设置字体
     *
     * @param writeSheetHolder writeSheetHolder
     * @param writeTableHolder writeTableHolder
     * @param row              行
     * @param relativeRowIndex 行索引
     * @param isHead           是头
     */
    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {
        if (!isHead) {
            //创建字体样式
            CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle();
            //获取当前行所有列
            int size = row.getPhysicalNumberOfCells();
            //遍历所有列
            IntStream.rangeClosed(0, size - 1).forEach(i -> {
                Cell cell = row.getCell(i);
                // 克隆原来的样式，只修改字体大小，其他不变
                cellStyle.cloneStyleFrom(cell.getCellStyle());
                Font font = writeSheetHolder.getSheet().getWorkbook().createFont();
                // 设置字体大小  12号
                font.setFontHeight((short) (12 * 20));
                // 设置字体
                font.setFontName("宋体");
                cellStyle.setFont(font);

                // 设置边框
                cellStyle.setBorderLeft(BorderStyle.THIN);
                cellStyle.setBorderRight(BorderStyle.THIN);
                cellStyle.setBorderBottom(BorderStyle.THIN);

                // 居中对齐
                cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

                cell.setCellStyle(cellStyle);//设置字体样式
            });
        }
    }
}